/*
 * Copyright 2011 myTDev.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mytdev.commons.text.parsers;

import com.mytdev.commons.text.AbstractParser;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 *
 * @author Yann D'Isanto
 */
public class DateFormatParser extends AbstractParser<DateFormat> {

    public static final String STYLE_DEFAULT = "default";
    public static final String STYLE_FULL = "full";
    public static final String STYLE_LONG = "long";
    public static final String STYLE_MEDIUM = "medium";
    public static final String STYLE_SHORT = "short";
    public static final String TYPE_DATE = "date";
    public static final String TYPE_TIME = "time";
    public static final String TYPE_DATE_TIME = "date-time";
    private static final Map<String, Integer> DATE_FORMAT_STYLES = new HashMap<String, Integer>();
    private static final Map<String, Type> DATE_FORMAT_TYPES = new HashMap<String, Type>();
    private static final Pattern DATE_FORMAT_DEFAULT_PATTERN = initDateFormatDefaultPattern();

    static {
        DATE_FORMAT_STYLES.put(STYLE_DEFAULT, DateFormat.DEFAULT);
        DATE_FORMAT_STYLES.put(STYLE_FULL, DateFormat.FULL);
        DATE_FORMAT_STYLES.put(STYLE_LONG, DateFormat.LONG);
        DATE_FORMAT_STYLES.put(STYLE_MEDIUM, DateFormat.MEDIUM);
        DATE_FORMAT_STYLES.put(STYLE_SHORT, DateFormat.SHORT);

        DATE_FORMAT_TYPES.put("date", Type.DATE);
        DATE_FORMAT_TYPES.put("time", Type.TIME);
        DATE_FORMAT_TYPES.put("date-time", Type.DATE_TIME);
    }

    public DateFormatParser() {
        super(DateFormat.class);
    }

    @Override
    public DateFormat parse(String string) throws ParseException {
        DateFormat dateFormat = null;
        if (DATE_FORMAT_DEFAULT_PATTERN.matcher(string).matches()) {
            String[] data = string.split(" ");
            Integer dateFormatStyle = DateFormat.DEFAULT;
            int typeIndex = 0;
            if(data.length == 2) {
                dateFormatStyle = DATE_FORMAT_STYLES.get(data[0].toLowerCase());
                typeIndex ++;
            }
            Type type = DATE_FORMAT_TYPES.get(data[typeIndex].toLowerCase());
            switch (type) {
                case DATE:
                    dateFormat = DateFormat.getDateInstance(dateFormatStyle);
                    break;
                case DATE_TIME:
                    dateFormat = DateFormat.getDateTimeInstance(dateFormatStyle, dateFormatStyle);
                    break;
                case TIME:
                    dateFormat = DateFormat.getTimeInstance(dateFormatStyle);
                    break;
            }
        } else {
            try {
                dateFormat = new SimpleDateFormat(string);
            } catch (IllegalArgumentException ex) {
                throw new ParseException(ex.getLocalizedMessage(), 0);
            }
        }
        return dateFormat;
    }

    private static Pattern initDateFormatDefaultPattern() {
        StringBuilder sb = new StringBuilder();
        sb.append("(((");
        sb.append(Pattern.quote(STYLE_DEFAULT));
        sb.append(")|(");
        sb.append(Pattern.quote(STYLE_FULL));
        sb.append(")|(");
        sb.append(Pattern.quote(STYLE_LONG));
        sb.append(")|(");
        sb.append(Pattern.quote(STYLE_MEDIUM));
        sb.append(")|(");
        sb.append(Pattern.quote(STYLE_SHORT));
        sb.append("))\\s+)?((");
        sb.append(Pattern.quote(TYPE_DATE));
        sb.append(")|(");
        sb.append(Pattern.quote(TYPE_DATE_TIME));
        sb.append(")|(");
        sb.append(Pattern.quote(TYPE_TIME));
        sb.append("))");
        return Pattern.compile(sb.toString(), Pattern.CASE_INSENSITIVE);
    }

    private enum Type {

        DATE,
        DATE_TIME,
        TIME
    }
}
